package com.example.chessbattleplatform.utils.CacheDB;

import cn.hutool.json.JSONUtil;
import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Exception.StatusCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
// 使用 Spring 的 CacheManager
public class JCacheEhcache implements ManuallyCacheDBUtils {

    private final CacheManager cacheManager; // 注入 Spring 的 CacheManager


    @Override
    public void putJsonData(String key, Object value, String CacheName) {
        //转化为json
        String jsonStr = JSONUtil.toJsonStr(value);

        // 判断 jsonStr 是否为空或空字符串
        if (jsonStr == null || jsonStr.isEmpty()) {
            return;
        }

        // 获取缓存实例
        Cache cache = cacheManager.getCache(CacheName);
        if (cache == null) {
            return;
        }

        try {
            // 将键值对写入 RocksDB
            cache.put(key, jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCode.ERROR_CODE, "添加缓存出错,请尝试再次尝试");
        }

    }

    @Override
    public <T> T getRocksDBToBean(String key, Class<T> beanClass, String CacheName) {

        // 获取缓存实例
        Cache cache = cacheManager.getCache(CacheName);

        if (cache == null) {
            return null;
        }

        try {
            // 获取缓存中的值
            String value = cache.get(key, String.class);

            // 判断值是否为空或空字符串
            if (value == null || value.isEmpty()) {
                return null;
            }

            // 将值转换为指定类型
            return JSONUtil.toBean(value, beanClass);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCode.ERROR_CODE, "查询缓存出错,请尝试再次尝试");
        }
    }

    @Override
    public boolean isExist(String key, String CacheName) {

        // 获取缓存实例
        Cache cache = cacheManager.getCache(CacheName);

        if (cache == null) {
            return false;
        }
        try {
            // 获取缓存中的值
            String value = cache.get(key, String.class);

            // 判断值是否为空或空字符串
            // 如果值不为空，则返回 true，否则返回 false
            return value != null && !value.isEmpty();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCode.ERROR_CODE, "查询缓存出错,请尝试再次尝试");
        }
    }

    @Override
    public void delete(String key, String CacheName) {
        // 获取缓存实例
        Cache cache = cacheManager.getCache(CacheName);
        if (cache == null) {
            return;
        }
        try {
            // 删除指定键
            cache.evict(key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(StatusCode.ERROR_CODE, "删除缓存出错,请尝试再次删除");
        }
    }
}
